package com.dupt.web.controller.material;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.dupt.common.config.DUPTConfig;
import com.dupt.system.domain.vo.MaterialDetialVo;
import com.sun.org.apache.bcel.internal.generic.DUP;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dupt.common.annotation.Log;
import com.dupt.common.core.controller.BaseController;
import com.dupt.common.core.domain.AjaxResult;
import com.dupt.common.enums.BusinessType;
import com.dupt.system.domain.MaterialDetial;
import com.dupt.system.service.IMaterialDetialService;
import com.dupt.common.utils.poi.ExcelUtil;
import com.dupt.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料明细Controller
 * 
 * @author zhuanyu.sun
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/materialDetial")
public class MaterialDetialController extends BaseController
{
    @Autowired
    private IMaterialDetialService materialDetialService;

    /**
     * 查询物料明细列表
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialDetial materialDetial)
    {
        startPage();
        List<MaterialDetialVo> list = materialDetialService.selectMaterialDetialList(materialDetial);
        return getDataTable(list);
    }


//    @PostMapping("/importData")
//    @ResponseBody
//    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
//    {
//        ExcelUtil<MaterialParam> util = new ExcelUtil<MaterialParam>(MaterialParam.class);
//        List<MaterialParam> detialList = util.importExcel(file.getInputStream());
//        String operName = SecurityUtils.getLoginUser().getUsername();
//        String message = materialDetialService.importMaterialDetial(detialList, updateSupport, operName);
//        return AjaxResult.success(message);
//    }

    /**
     * 导出物料明细列表
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:export')")
    @Log(title = "物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialDetial materialDetial)
    {
        List<MaterialDetialVo> list = materialDetialService.selectMaterialDetialList(materialDetial);
        ExcelUtil<MaterialDetialVo> util = new ExcelUtil<MaterialDetialVo>(MaterialDetialVo.class);
        util.exportExcel(response, list, "物料明细数据");
    }

    /**
     * 获取物料明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:query')")
    @GetMapping(value = "/{materialDetialId}")
    public AjaxResult getInfo(@PathVariable("materialDetialId") Long materialDetialId)
    {
        return success(materialDetialService.selectMaterialDetialByMaterialDetialId(materialDetialId));
    }

    /**
     * 获取物料明细详细信息
     */
//    @PreAuthorize("@ss.hasPermi('materialDetial:materialInnerCode:query')")
//    @GetMapping(value = "/materialInnerCode/{materialInnerCode}")
//    public AjaxResult getInfoByMaterialInnerCode(@PathVariable("materialInnerCode") String materialInnerCode)
//    {
//        return success(materialDetialService.selectMaterialDetialByMaterialInnerCode(materialInnerCode));
//    }

    /**
     * 根据物料编码查询物料信息
     */
//    @PreAuthorize("@ss.hasPermi('materialDetial:materialCode:query')")
//    @GetMapping(value = "/materialCode/{materialCode}")
//    public AjaxResult getInfoByMaterialCode(@PathVariable("materialCode") String materialCode)
//    {
//        return success(materialDetialService.selectMaterialDetialByMaterialCode(materialCode));
//    }

    /**
     * 新增物料明细
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:add')")
    @Log(title = "物料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam(value="materialDetialVo",required=false)String materialDetialVo,
                          @RequestParam(value="files",required=false) List<MultipartFile> files)  throws IOException
    {
        return toAjax(materialDetialService.insertMaterialDetial(materialDetialVo,files));
    }

    /**
     * 修改物料明细
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestParam(value="materialDetialVo",required=false)String materialDetialVo,
                           @RequestParam(value="files",required=false) List<MultipartFile> files) throws IOException
    {
        return toAjax(materialDetialService.updateMaterialDetial(materialDetialVo,files));
    }

    /**
     * 删除物料明细
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:remove')")
    @Log(title = "物料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialDetialIds}")
    public AjaxResult remove(@PathVariable Long[] materialDetialIds)
    {
        return toAjax(materialDetialService.deleteMaterialDetialByMaterialDetialIds(materialDetialIds));
    }

    /**
     * 文件预览
     */
    @PreAuthorize("@ss.hasPermi('materialDetial:preview')")
    @GetMapping("/preview/{file}/{materialDetialId}")
    @Log(title = "质量追溯", businessType = BusinessType.DOWNANDPREVIEW)
    public void previewFile(@PathVariable("file") String file,@PathVariable("materialDetialId") Long materialDetialId, HttpServletResponse response) {
        try {
            // 根据qualityId获取文件信息
            MaterialDetialVo detialVo = materialDetialService.selectMaterialDetialByMaterialDetialId(materialDetialId);
            // 获取文件绝对路径
            String filePath =  DUPTConfig.getUploadPath()+detialVo.getFilePath();
            File localFile = new File(filePath+file);
            if (!localFile.exists()) {
                throw new RuntimeException("文件不存在");
            }

            ServletOutputStream outputStream = response.getOutputStream();
            InputStream in = new FileInputStream(localFile);// 读取文件
            int i = IOUtils.copy(in, outputStream); // copy流数据,i为字节数
            in.close();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("文件预览失败", e);
        }
    }
}
